/* 筛选数据 */
import shopList from "./listCreate.js"; // 商品列表内容
export const selectSetOption = function (selectList = [], selectTitle, isSort) {
    /* 函数传入选中的数组 从shopList中筛选并返回结果
        selectList区间 : 1~10
    */
    let newShopList = selectList.length === 0 ? shopList : [];
    if (selectList.length !== 0)
            /* 匹配类型相同的数据添加到新的list中 */ {
        shopList.forEach(item => {
            let index = selectList.findIndex(ele => item.selectType === ele);
            if (index !== -1) {
                newShopList.push(item)
            }
        })
    }

    if (selectTitle) {
        let selectAs = [{ title: "综合", key: "synthesize" }, { title: "销量", key: "salesVolume" }, { title: "价格", key: "pic" }]
        let index = selectAs.findIndex(item => item.title === selectTitle);
        if (index != -1) {
            /* 按照不同的点击进行排序 */
            newShopList.sort((a, b) => (isSort ? a : b)[selectAs[index].key] - (!isSort ? a : b)[selectAs[index].key])
        }
    }
    return newShopList;
}

/* 分页计算函数 dataList=要分页的数组 pageNumber=页码 pageSize=页长 */
export const pageInfoSet = function (dataList, pageNumber, pageSize) {
    let newList = [];
    let start = (pageNumber - 1) * pageSize;// 跳过多少条数据(分页开始位置)
    let end = (pageNumber * pageSize) - 1;// 跳过多少条数据(分页结束位置)
    dataList.forEach((item, index) => {
        if (index >= start && index <= end) {
            newList.push(item);
        }
    })
    return newList;
}